package com.yangguang.huahui.ui.activitys;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.CryptoUtil;
import com.yangguang.huahui.utils.SharedPreferenceUtil;

public class WelcomeActivity extends BaseActivity {

    @Override
    protected int initLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);

        getArea();
        try {
            if (!CommonUtil.isNull(DataInfo.TOKEN)) {
                loginEasemob(DataInfo.TOKEN, CryptoUtil.md5(SharedPreferenceUtil.getInstance(this).getString(SharedPreferenceUtil.PASSWORD) + "userKey"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonUtil.myToastA(this, "username or password is null or empty!");
        }
        requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
                , Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x0001);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void permissionSuccess(int requestCode) {
        super.permissionSuccess(requestCode);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                finish();
            }
        }, 3000);
    }

    public void loginEasemob(String userName, String password) {
        EMClient.getInstance().login(userName, password, new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                Log.e("main", "登录聊天服务器成功！");
                EMClient.getInstance().chatManager().loadAllConversations();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！");
            }
        });
    }
}
